package com.ruoyi.xzs.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.xzs.domain.KnowsTree;
import com.ruoyi.xzs.service.IKnowsTreeService;
import com.ruoyi.xzs.vo.KnowQuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 知识点树Controller
 * 
 * @author li
 * @date 2023-03-14
 */
@RestController
@RequestMapping("/tree")
public class KnowsTreeController extends BaseController
{
    @Autowired
    private IKnowsTreeService knowsTreeService;

    /**
     * 查询知识点树列表
     */
    @RequiresPermissions("xzs:tree:list")
    @GetMapping("/list")
    public AjaxResult list(KnowsTree knowsTree)
    {
        List<KnowsTree> list = knowsTreeService.selectKnowsTreeList(knowsTree);
        return success(list);
    }

    /**
     * 导出知识点树列表
     */
    @RequiresPermissions("xzs:tree:export")
    @Log(title = "知识点树", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KnowsTree knowsTree)
    {
        List<KnowsTree> list = knowsTreeService.selectKnowsTreeList(knowsTree);
        ExcelUtil<KnowsTree> util = new ExcelUtil<KnowsTree>(KnowsTree.class);
        util.exportExcel(response, list, "知识点树数据");
    }

    /**
     * 获取知识点树详细信息
     */
    @RequiresPermissions("xzs:tree:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(knowsTreeService.selectKnowsTreeById(id));
    }

    /**
     * 新增知识点树
     */
    @RequiresPermissions("xzs:tree:add")
    @Log(title = "知识点树", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowsTree knowsTree)
    {
        return toAjax(knowsTreeService.insertKnowsTree(knowsTree));
    }

    /**
     * 修改知识点树
     */
    @RequiresPermissions("xzs:tree:edit")
    @Log(title = "知识点树", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowsTree knowsTree)
    {
        return toAjax(knowsTreeService.updateKnowsTree(knowsTree));
    }

    /**
     * 删除知识点树
     */
    @RequiresPermissions("xzs:tree:remove")
    @Log(title = "知识点树", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(knowsTreeService.deleteKnowsTreeByIds(ids));
    }

    /**
     * 获取知识点树及其所包含的题目
     */
    @RequiresPermissions("xzs:tree:treeQuestion")
    @GetMapping("/treeQuestion")
    public AjaxResult treeQuestion(String kName){
        List<KnowQuestionVO> list = knowsTreeService.selectTreeQuestion(kName);
        return success(list);
    }

    /**
     * 查询已存在题目的知识点树及所包含题目
     * @return
     */
    @RequiresPermissions("xzs:tree:getAllWithTreeAndQuestion")
    @GetMapping("/getAllWithTreeAndQuestion")
    public AjaxResult getAllWithTreeAndQuestion(){
        List<KnowQuestionVO> list = knowsTreeService.getAllWithTreeAndQuestion();
        return success(list);
    }

    /**
     * 知识点树递归
     * @return
     */
    @RequiresPermissions("xzs:tree:getTreeQuestion")
    @GetMapping("/getTreeQuestion")
    public AjaxResult getTreeQuestion(){
        List<KnowQuestionVO> treeQuestion = knowsTreeService.getTreeQuestion();
        return success(treeQuestion);
    }


}
